package com.kitty.game.welfare.facade;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.role.model.Role;
import com.kitty.game.welfare.message.vo.ShenmiGift;
import com.kitty.game.welfare.service.ConsumeScoreRewardHandler;
import com.kitty.game.welfare.service.OnlineRewardHandler;
import com.kitty.game.welfare.service.RechargeScoreRewardHandler;
import com.kitty.game.welfare.service.WelfareService;
import com.kitty.listener.EventType;
import com.kitty.listener.annotation.EventHandler;
import com.kitty.listener.event.HeartBeatEvent;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.nutz.lang.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
//后写
import com.kitty.game.welfare.message.ReqBuyRechargeScoreGoods;
import com.kitty.game.welfare.message.ReqDoReplenishSign;
import com.kitty.game.welfare.message.ReqGetActiveBonusInfo;
import com.kitty.game.welfare.message.ReqNewLotteryDraw;
import com.kitty.game.welfare.message.ReqNewLotteryFetch;
import com.kitty.game.welfare.message.ReqNewLotteryInfo;
import com.kitty.game.welfare.message.ReqRequestConsumeScoreGoods;
import com.kitty.game.welfare.message.ReqRequestRechargeScoreGoods;
import com.kitty.game.welfare.message.ReqShenmiDaliPick;
import com.kitty.game.welfare.message.ReqShenmiGift;
import com.kitty.game.welfare.message.RespActiveBonusInfo;
import com.kitty.game.welfare.message.RespShenmiGift;


@Controller
@Slf4j
public class WelfareController {


    @RequestMapping
    public void activeBonusInfo(IoSession session, ReqGetActiveBonusInfo reqGetActiveBonusInfo) {
        MessagePusher.pushMessage(session, new RespActiveBonusInfo());
    }

    /**
     * 神秘大礼
     *
     * @param session
     */
    @RequestMapping
    public void shenmiGift(IoSession session, ReqShenmiGift reqShenmiGift) {
        List<Integer> times = Arrays.asList(5 * 60, 15 * 60, 30 * 60, 50 * 60, 80 * 60, 120 * 60, 170 * 60, 230 * 60);
        Role role = SessionUtils.getRoleBySession(session);
        List<ShenmiGift> list = new ArrayList<>();
        Map<Byte, String> zadan = role.getActivity().getZadan();
        if (zadan != null) {
            for (byte i = 1; i <= 8; i++) {
                ShenmiGift shenmiGift = new ShenmiGift();
                String content = zadan.get(i);
                if (!Strings.isEmpty(content)) {
                    shenmiGift.setTime(times.get(i - 1).shortValue());
                    shenmiGift.setName(content.split(":")[0]);
                    shenmiGift.setBrate(Byte.parseByte(content.split(":")[1]));
                    shenmiGift.setPos(i);
                } else {
                    shenmiGift.setPos(i);
                    shenmiGift.setTime(times.get(i - 1).shortValue());
                }
                list.add(shenmiGift);
            }
        } else {

            for (byte i = 1; i <= 8; i++) {
                ShenmiGift shenmiGift = new ShenmiGift();
                shenmiGift.setPos(i);
                shenmiGift.setTime(times.get(i - 1).shortValue());
                list.add(shenmiGift);
            }
        }
        RespShenmiGift respShenmiGift = new RespShenmiGift();
        respShenmiGift.setOnline_time(SpringUtils.getRoleService().getOnlineSec(role));
        respShenmiGift.setList(list);
        MessagePusher.pushMessage(session, respShenmiGift);
    }

    @RequestMapping
    public void openChoujiangList(IoSession session, ReqNewLotteryInfo reqNewLotteryInfo) {
        SpringUtils.getBean(WelfareService.class).openChoujiangList(session, reqNewLotteryInfo);
    }

    @RequestMapping
    public void choujiang(IoSession session, ReqNewLotteryDraw newLotteryDraw) {
        SpringUtils.getBean(WelfareService.class).lotteryDraw(session, newLotteryDraw);
    }

    @RequestMapping
    public void choujiangResult(IoSession session, ReqNewLotteryFetch reqNewLotteryFetch) {
        log.info("========choujiangResult===");
    }

    /**
     * 补签
     */
    @RequestMapping
    public void buqian(IoSession session, ReqDoReplenishSign reqDoReplenishSign) {
        SpringUtils.getBean(WelfareService.class).doReplenishSign(session, reqDoReplenishSign);
    }

    /**
     * 砸蛋
     */
    @RequestMapping
    public void zadan(IoSession session, ReqShenmiDaliPick reqShenmiDaliPick) {
        log.info("砸蛋=={}", reqShenmiDaliPick.getPos());
        SpringUtils.getBean(WelfareService.class).zadan(session, reqShenmiDaliPick);
    }

    /**
     * 请求消费积分奖励内容
     */
    @RequestMapping
    public void reqConsumeScoreGoods(IoSession session, ReqRequestConsumeScoreGoods reqRequestConsumeScoreGoods) {
        Role role = SessionUtils.getRoleBySession(session);
        SpringUtils.getBean(ConsumeScoreRewardHandler.class).viewConsumeScoreGoods(role);
    }

    /**
     * 请求充值积分奖励内容
     */
    @RequestMapping
    public void reqRechargeScoreGoods(IoSession session, ReqRequestRechargeScoreGoods reqRequestRechargeScoreGoods) {
        Role role = SessionUtils.getRoleBySession(session);
        SpringUtils.getBean(RechargeScoreRewardHandler.class).viewRechargeScoreGoods(role);
    }

    @RequestMapping
    public void reqBuyRechargeScoreGoods(IoSession session, ReqBuyRechargeScoreGoods reqBuyRechargeScoreGoods) {
        Role role = SessionUtils.getRoleBySession(session);
        SpringUtils.getBean(RechargeScoreRewardHandler.class).buyRechargeScoreGoods(role, reqBuyRechargeScoreGoods.getNo(), reqBuyRechargeScoreGoods.getNum());
    }

    @EventHandler(EventType.HEART_BEAT)
    public void handleHeartBeat(HeartBeatEvent heartBeatEvent) {
        SpringUtils.getBean(OnlineRewardHandler.class).handleHeartBeart(heartBeatEvent.getRole(), heartBeatEvent.getPrevHeartTime());
    }
}
